package com.handpay.nocardpay.baselibrary.notification.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.notification.model.bean.NoticeBody;
import com.handpay.nocardpay.baselibrary.notification.view.fragment.NoticeDetailFragment;
import com.handpay.nocardpay.baselibrary.utils.AccountUtils;
import com.handpay.nocardpay.baselibrary.utils.CommonUtils;
import com.handpay.nocardpay.baselibrary.utils.DialogUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by fplei on 2017/8/21.
 * 这里暂时这么写，有时间在统一继承BaseRecycleAdapter
 */

public class NoticeListAdapter extends RecyclerView.Adapter<NoticeListAdapter.ItemViewHolder>{
    private List<NoticeBody> noticeInfos = new ArrayList<>();
    private static final int TYPE_ITEM = 0;

    private OnItemClickListener itemClickListener=null;
    private Context context;
    public NoticeListAdapter(Context mContext,OnItemClickListener mtemClickListener){
        this.itemClickListener=mtemClickListener;
        this.context=mContext;
    }
    public List<NoticeBody> getList() {
        return noticeInfos;
    }
    @Override
    public int getItemCount() {
        if (noticeInfos == null) {
            return 0;
        } else {
            return noticeInfos.size();
        }
    }


    @Override
    public int getItemViewType(int position) {

        return TYPE_ITEM;

    }

    /**
     * 界面设置及逻辑处理
     *
     * @param holder   ItemViewHolder
     * @param position 位置
     */
    @Override
    public void onBindViewHolder(final ItemViewHolder holder, final int position) {
        NoticeBody notice = noticeInfos.get(position);
        if (notice.isRead()) {
            holder.itemView.setVisibility(View.INVISIBLE);
        } else {
            holder.itemView.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(notice.getNoticeId())) {
            holder.itemTitle.setText(notice.getTitle());
            holder.itemText.setText(notice.getContent().replaceAll("&", "\n\r"));
            holder.itemTime.setText(CommonUtils.formatYearMMDay(notice.getTime()));
            holder.ll_item.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    if(itemClickListener!=null){
                        NoticeBody info = noticeInfos.get(position);
                        itemClickListener.onClick(v,info);
                    }
                }
            });
            holder.ll_item.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if(itemClickListener!=null){
                        NoticeBody info = noticeInfos.get(position);
                        itemClickListener.onLongClick(v,info);
                    }
                    return true;
                }
            });
        }
    }

    /**
     * ViewHolder 的绑定
     *
     * @param parent   parent
     * @param viewType 可以实现多布局
     * @return MyBaseAdapter.ItemViewHolder
     */
    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder;
        View view = null;
        if (viewType == TYPE_ITEM) {
            view = LayoutInflater.from(context).inflate(R.layout.item_notice, null);
            view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
        }
        itemViewHolder = new ItemViewHolder(view);
        return itemViewHolder;
    }

    public void setData(List<NoticeBody> mData) {
        this.noticeInfos = mData;
        notifyDataSetChanged();
    }

    /**
     * 正常的Item
     */
    class ItemViewHolder extends RecyclerView.ViewHolder {
        LinearLayout ll_item;
        ImageView itemView;
        TextView itemTitle;
        TextView itemText;
        TextView itemTime;

        ItemViewHolder(View view) {
            super(view);
            ll_item = (LinearLayout) view.findViewById(R.id.ll_item);
            itemView = (ImageView) view.findViewById(R.id.notice_item_newview);
            itemTitle = (TextView) view.findViewById(R.id.notice_item_title);
            itemText = (TextView) view.findViewById(R.id.notice_item_text);
            itemTime = (TextView) view.findViewById(R.id.notice_item_time);
        }
    }
    public interface OnItemClickListener{
        /**
         * 通知条目点击
         * @param v
         * @param noticeBody
         */
        void onClick(View v,NoticeBody noticeBody);

        /**
         * 通知条目长按
         * @param v
         * @param noticeBody
         */
        void onLongClick(View v,NoticeBody noticeBody);
    }
}
